class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> ret=new ArrayList<Integer>();
        char[] ss=s.toCharArray();
        char[] pp=p.toCharArray(); 
        int[] hash1=new int[26];
        for(char ch:pp) hash1[ch-'a']++;
        int[] hash2=new int[26];
        int left=0,right=0,count=0;
        int n=ss.length,m=pp.length;
        while(right<n){
            if(++hash2[ss[right]-'a']<=hash1[ss[right]-'a']) count++;
            if(right-left+1>m){
                char leftChar = ss[left++];
                if(hash2[leftChar-'a']--<=hash1[leftChar-'a']){
                    count--;
                } 
            }
            if(count==m) ret.add(left);
            right++;
        }
        return ret;
    }
}